import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from "../pages/Home";
import Login from '../pages/Login'
import IndexMain from "../views/indexMain";
import langIndex from "../i18n/lang/index";
Vue.use(VueRouter)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/',
    component: Home,
    children:[
      {
        path: '/',
        name: 'indexMain',
        component: IndexMain,
        meta:{
          pageTitle: 'indexTitle', //页面title
        }
      }
    ]
  },
  {
    path: '/Login',
    name: 'Login',
    component: Login,
    meta:{
      requireAuth: true //不需要登陆就可以进入页面
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});
/*******
 * 全局路由守卫
 * 处理是否需要登陆或者是需要特殊处理的页面
 * requireAuth是否需要登录才能进入的页面,requireAuth为true不需要登陆。没设置或者为false需要登陆
 ****/
router.beforeEach((to, from, next) => {
  //设置title
  if(to.meta.pageTitle){
    const langType = localStorage.getItem('lang') || 'cn';
    const langObj = langIndex[langType];
    document.title = langObj[to.meta.pageTitle];
  }
  //路由拦截需要登录的页面
  if(!to.meta.requireAuth){
    const isInto = sessionStorage.getItem('userLoginInfo') || null;
    if(isInto){
      next();
    }else {
      next({
        path:'/Login',
        query: {redirect: to.fullPath}
      });
    }
  }else {
    next();
  }
});

export default router
